using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace LdExpress
{
    public class Ports
    {
        public long ID = -1;
        public string Name = "";
        public string Code = "";
        public string Description = "";
        public string City = "";
        public string CountryCode = "";
        public string Country = "";
        public string Region = "";
        public string ServiceAreaCode = "";
        public int Type = 0;
        public int Sort = 0;
        public int IsLocalUsed = 0;
        public DateTime LastUpdate = DateTime.Now;


        public Ports()
        {
        }

        public Ports(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_ports";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "Insert into " + strTableName + " (" +
                             "`Name`" +
                             ",`Code`" +
                             ",`Description`" +
                             ",`City`" +
                             ",`CountryCode`" +
                             ",`Country`" +
                             ",`Region`" +
                             ",`ServiceAreaCode`" +
                             ",`Type`" +
                             ",`Sort`" +
                              ",`LastUpdate`" +
                              ",`IsLocalUsed`" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(Name) + "'" +
                             "," + "'" + DBModule.RefineString(Code) + "'" +
                             "," + "'" + DBModule.RefineString(Description) + "'" +
                             "," + "'" + DBModule.RefineString(City) + "'" +
                             "," + "'" + DBModule.RefineString(CountryCode) + "'" +
                             "," + "'" + DBModule.RefineString(Country) + "'" +
                             "," + "'" + DBModule.RefineString(Region) + "'" +
                             "," + "'" + DBModule.RefineString(ServiceAreaCode) + "'" +
                             "," + Type.ToString() +
                             "," + Sort.ToString() +
                             "," + DBModule.RefineDatetime(LastUpdate) +
                             "," + IsLocalUsed.ToString() +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Ports", "add", cn, trans);
                //GV.Response.Write(strSQL);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "Name='" + DBModule.RefineString(Name) + "'" +
                             ",Code='" + DBModule.RefineString(Code) + "'" +
                             ",Description='" + DBModule.RefineString(Description) + "'" +
                             ",City='" + DBModule.RefineString(City) + "'" +
                             ",CountryCode='" + DBModule.RefineString(CountryCode) + "'" +
                             ",Country='" + DBModule.RefineString(Country) + "'" +
                             ",Region='" + DBModule.RefineString(Region) + "'" +
                             ",ServiceAreaCode='" + DBModule.RefineString(ServiceAreaCode) + "'" +
                             ",Type = " + Type.ToString() +
                             ",Sort = " + Sort.ToString() +
                             ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "" +
                             ",IsLocalUsed = " + IsLocalUsed.ToString() +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Ports", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_ports WHERE ID = " + this.ID.ToString();

            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("Ports", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_ports WHERE ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Name")) Name = dr["Name"].ToString();
                if (!dr.IsNull("Code")) Code = dr["Code"].ToString();
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("City")) City = dr["City"].ToString();
                if (!dr.IsNull("CountryCode")) CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("Country")) Country = dr["Country"].ToString();
                if (!dr.IsNull("Region")) Region = dr["Region"].ToString();
                if (!dr.IsNull("ServiceAreaCode")) ServiceAreaCode = dr["ServiceAreaCode"].ToString();
                if (!dr.IsNull("Type")) Type = int.Parse(dr["Type"].ToString());
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("IsLocalUsed")) IsLocalUsed = int.Parse(dr["IsLocalUsed"].ToString());
            }
        }
        public void Load(string portName, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_ports WHERE Name = '" + DBModule.RefineString(portName) + "' limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Name")) Name = dr["Name"].ToString();
                if (!dr.IsNull("Code")) Code = dr["Code"].ToString();
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("City")) City = dr["City"].ToString();
                if (!dr.IsNull("CountryCode")) CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("Country")) Country = dr["Country"].ToString();
                if (!dr.IsNull("Region")) Region = dr["Region"].ToString();
                if (!dr.IsNull("ServiceAreaCode")) ServiceAreaCode = dr["ServiceAreaCode"].ToString();
                if (!dr.IsNull("Type")) Type = int.Parse(dr["Type"].ToString());
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("IsLocalUsed")) IsLocalUsed = int.Parse(dr["IsLocalUsed"].ToString());
            }
        }
        public static void GetList(string strIDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select tbl_ports.*,tblCountry.CountryName as CountrySourceName, tbRegion.Name as RegionSourceName " +
                //",tblServiceAreas.Name as ServiceAreaName" +
                " FROM tbl_ports " +
                " left join tbl_countries tblCountry on  tbl_ports.CountryCode = tblCountry.CountryCode " +
                //" left join tbl_serviceareas tblServiceAreas on  tbl_ports.ServiceAreaCode = tblServiceAreas.Code " +
                " left join tbl_region tbRegion on  tblCountry.ServiceAreaID = tbRegion.ID " +
                "where 1=1 ";
            if ((strIDs != "") && (strIDs != null))
            {
                if (strIDs != "All")
                    strSQL += strIDs;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_ports where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
        public static string GetUniqueCode(long ID, string inputcode, MySqlConnection con, MySqlTransaction trans)
        {
            string retVal = inputcode;
            string sqlCommand = "";
            int count = 1;
            while (true)
            {
                sqlCommand = "Select * from tbl_ports where ID <> " + ID.ToString() + " And code='" + DBModule.RefineString(retVal) + "' limit 0,1";

                DataSet ds = DBModule.ExecuteQuery(sqlCommand, con, trans);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    break;
                }
                else
                {
                    retVal = inputcode + "-" + count;
                    count++;
                }
            }
            return retVal;
        }
        public static bool GetCheckCode(long ID, string inputcode, MySqlConnection con, MySqlTransaction trans)
        {
            bool bCode = false;
            string retVal = inputcode;
            string sqlCommand = "";
            int count = 1;

            sqlCommand = "Select * from tbl_ports where ID <> " + ID.ToString() + " And code='" + DBModule.RefineString(retVal) + "' limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(sqlCommand, con, trans);
            if (ds.Tables[0].Rows.Count > 0)
            {
                bCode = true;
            }
            return bCode;
        }

        public static long GetPortIDFromName(string name)
        {
            string sqlCommand = "select ID from tbl_ports where name ='" + DBModule.RefineString(name) + "' limit 0,1;";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
            if (ds.Tables[0].Rows.Count > 0)
                return long.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
            else
            {
                return 0;
            }
        }

        public static string GetPortNameFromID(long ID)
        {
            string sqlCommand = "select name from tbl_ports where ID =" + ID + " limit 0,1;";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
            if (ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0]["name"].ToString();
            else
            {
                return "";
            }
        }
    }
}